package top.forforever.srb.sms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.forforever.srb.common.exception.Assert;
import top.forforever.srb.common.result.ResponseEnum;
import top.forforever.srb.common.util.RandomUtils;
import top.forforever.srb.common.util.RegexValidateUtils;
import top.forforever.srb.mq.pojo.dto.SmsDTO;
import top.forforever.srb.sms.client.ServiceCoreUserInfoClient;
import top.forforever.srb.sms.prop.SmsProperties;
import top.forforever.srb.sms.service.ApiSmsService;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @create: 2023/5/3
 * @Description:
 * @FileName: ApiSmsServiceImpl
 * @自定义内容：
 */
@Service
public class ApiSmsServiceImpl implements ApiSmsService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ServiceCoreUserInfoClient serviceCoreUserInfoClient;

    @Override
    public void sendCode(String mobile) {
        //判断手机号码不能为空
        Assert.notNull(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //验证手机号格式
        boolean cellphone = RegexValidateUtils.checkCellphone(mobile);
        Assert.isTrue(cellphone, ResponseEnum.MOBILE_ERROR);
        //判断手机号是否已被注册
        Boolean mobileExist = serviceCoreUserInfoClient.isMobileExist(mobile);
        Assert.isTrue(!mobileExist,ResponseEnum.MOBILE_EXIST_ERROR);

        //生成验证码
        String code = RandomUtils.getSixBitRandom();

        //公共配置
        DefaultProfile defaultProfile =
                DefaultProfile.getProfile(SmsProperties.REGION_ID, SmsProperties.KEY_ID, SmsProperties.KEY_SECRET);

        //发送短信
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置请求参数
        CommonRequest commonRequest = setCommonRequest(mobile, code);

        CommonResponse commonResponse = null;
        try {
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        boolean success = commonResponse.getHttpResponse().isSuccess();
        Assert.isTrue(success, ResponseEnum.ERROR);

        // 将验证码存入redis
        System.out.println(code);
        redisTemplate.opsForValue().set("srb:sms:code:" + mobile, code, 30, TimeUnit.DAYS);

    }

    @Override
    public void sendRechargeSuccess(SmsDTO smsDTO) {
        //判断手机号不能为空

        //判断手机号格式是否正确


        System.out.println("发送短信成功");
    }

    private CommonRequest setCommonRequest(String mobile, String code) {
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.putQueryParameter("PhoneNumbers", mobile);
        commonRequest.putQueryParameter("SignName", SmsProperties.SIGN_NAME);
        commonRequest.putQueryParameter("TemplateCode", SmsProperties.TEMPLATE_CODE);
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        //new Gson().toJson(map)
        commonRequest.putQueryParameter("TemplateParam", new JSONObject().toJSONString(map));
        commonRequest.setSysVersion("2017-05-25");
        commonRequest.setSysDomain(SmsProperties.SYS_DOMAIN);
        commonRequest.setSysAction(SmsProperties.SYS_ACTION);
        commonRequest.setSysMethod(MethodType.POST);
        return commonRequest;
    }

}
